package com.example.demo.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;

@Component
public class SingleMailSend {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${mail.fromMail,addrs}")
    private String from;

    public void sendSimpleMail(String email) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(message,true);
             message.setFrom(from);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("主题：我爱你");
            Calendar c = Calendar.getInstance();
            //现在的时间(单位：毫秒)
            //TODO:时间换算问题，如何处理int和long之间的关系
            long time = c.getTimeInMillis();

            //创建激活码
            String token=email+time;
            //过期时间为24小时后
//      int token_exptime=(int)(time+1000*60*60*24);
            String token_exptime=(time+1000*20)+"";    //这里测试是用的20秒

            String id= UUID.randomUUID().toString();
            StringBuffer sb=new StringBuffer("<div style=\"width:660px;overflow:hidden;border-bottom:1px solid #bdbdbe;\">"+"<div style=\"padding:24px 20px;\">您好，"+email+"<br/><br/>请点击下面链接激活账号，24小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</br>");
            sb.append("<a href=\"http://localhost:8000/mail/active?id=");
            sb.append(id);
            sb.append("&token=");
            sb.append(token);
            sb.append("\">http://localhost:8000/mail/active?id=");
            sb.append(id);
            sb.append("&token=");
            sb.append(token);
            sb.append("</a>"+"<br/>如果以上链接无法点击，请把上面网页地址复制到浏览器地址栏中打开<br/><br/><br/>" + "</div></div>" );
            mimeMessageHelper.setText(sb.toString(), true   );
            //mimeMessageHelper.setText("我爱你");
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        //message.setText("我也爱你，小曦");
        mailSender.send(message);
    }
    private void sendMail(String email) throws MessagingException {

        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost("smtp.163.com");
        // 建立邮件消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 设置收件人，寄件人 用数组发送多个邮件
        // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo(email);
        mailMessage.setFrom("your eamil");
        mailMessage.setSubject(" 测试简单文本邮件发送！ ");
        mailMessage.setText(" 测试我的简单邮件发送机制！！2222这是测试环境的代码 "+new Date());
        mailMessage.setSentDate(new Date());
        senderImpl.setPort(25);
        senderImpl.setUsername("your email"); // 根据自己的情况,设置username
        senderImpl.setPassword("password"); // 根据自己的情况, 设置password
        senderImpl.setDefaultEncoding("UTF-8");
        Properties prop = new Properties();
//        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        prop.put("mail.debug", "true");//便于调试
        prop.put("mail.transport.protocol", "smtp");
        /**
         * 关键代码，
         * 出现554 DT:SPM 163 smtp5,D9GowACHO7RNWNdXmXs1Bw--.9035S2 1473730639,
         * please see http://mail.163.com/help/help_spam_16.htm?ip=124.251.36.10
         * &hostid=smtp5&time=1473730639
         原因是：ip与域名不匹配。
         */
        prop.put("mail.smtp.localhost", "mail.digu.com");
        prop.put("mail.smtp.auth", "false");
        prop.put("mail.smtp.port", "25");
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件


        senderImpl.send(mailMessage);


        System.out.println(" 邮件发送成功.. ");

    }
}
